<!DOCTYPE html>





<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.9.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico?v=7.4.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=7.4.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico?v=7.4.0">
  <link rel="mask-icon" href="/images/logo.svg?v=7.4.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.4.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.4.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.json',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="在制作 Docker Images 之前, 我们先看一下Docker 官方提供了一些建议和准则，在大多数情况下建议遵守。  容器是短暂的，也就是说，你需要可以容易的创建、销毁、配置你的容器。  多数情况，构建镜像的时候是将 Dockerfile 和所需文件放在同一文件夹下。但为了构建性能，我们可以采用 .dockerignore 文件来排除文件和目录。  避免安装不必要的包，构建镜像应该尽可能减少">
<meta name="keywords" content="linux,docker">
<meta property="og:type" content="article">
<meta property="og:title" content="记录一次docker镜像的构建过程">
<meta property="og:url" content="https://unix2dos.github.io/p/2450240c.html">
<meta property="og:site_name" content="Levon&#39;s Blog">
<meta property="og:description" content="在制作 Docker Images 之前, 我们先看一下Docker 官方提供了一些建议和准则，在大多数情况下建议遵守。  容器是短暂的，也就是说，你需要可以容易的创建、销毁、配置你的容器。  多数情况，构建镜像的时候是将 Dockerfile 和所需文件放在同一文件夹下。但为了构建性能，我们可以采用 .dockerignore 文件来排除文件和目录。  避免安装不必要的包，构建镜像应该尽可能减少">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-09-27T15:36:51.133Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="记录一次docker镜像的构建过程">
<meta name="twitter:description" content="在制作 Docker Images 之前, 我们先看一下Docker 官方提供了一些建议和准则，在大多数情况下建议遵守。  容器是短暂的，也就是说，你需要可以容易的创建、销毁、配置你的容器。  多数情况，构建镜像的时候是将 Dockerfile 和所需文件放在同一文件夹下。但为了构建性能，我们可以采用 .dockerignore 文件来排除文件和目录。  避免安装不必要的包，构建镜像应该尽可能减少">
  <link rel="canonical" href="https://unix2dos.github.io/p/2450240c">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>记录一次docker镜像的构建过程 | Levon's Blog</title>
  








  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">
  <div class="container use-motion">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Levon's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <h1 class="site-subtitle" itemprop="description">微信:  L6241425</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>时光</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-categories">
      
    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-tags">
      
    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger">
        
          <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
      </li>
    
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
            

          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
      <article itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block post">
    <link itemprop="mainEntityOfPage" href="https://unix2dos.github.io/p/2450240c.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Levon">
      <meta itemprop="description" content="Never Give Up">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Levon's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">记录一次docker镜像的构建过程

          
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2019-08-24 20:54:46" itemprop="dateCreated datePublished" datetime="2019-08-24T20:54:46+08:00">2019-08-24</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-09-27 23:36:51" itemprop="dateModified" datetime="2019-09-27T23:36:51+08:00">2019-09-27</time>
              </span>
            
          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/2-linux系统/" itemprop="url" rel="index"><span itemprop="name">2-linux系统</span></a></span>

                
                
                  ，
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/2-linux系统/docker/" itemprop="url" rel="index"><span itemprop="name">docker</span></a></span>

                
                
              
            </span>
          

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
        
      
      <span class="post-meta-item-text">Disqus：</span>
    
    <a title="disqus" href="/p/2450240c.html#comments" itemprop="discussionUrl"><span class="post-comments-count disqus-comment-count" data-disqus-identifier="p/2450240c.html" itemprop="commentCount"></span></a>
  </span>
  
  
          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
              
                <span class="post-meta-item-text">本文字数：</span>
              
              <span>3.4k</span>
            </span>
          
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
              
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              
              <span>3 分钟</span>
            </span>
          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>在制作 Docker Images 之前, 我们先看一下Docker 官方提供了一些建议和准则，在大多数情况下建议遵守。</p>
<ul>
<li><p>容器是短暂的，也就是说，你需要可以容易的创建、销毁、配置你的容器。</p>
</li>
<li><p>多数情况，构建镜像的时候是将 Dockerfile 和所需文件放在同一文件夹下。但为了构建性能，我们可以采用 <a href="https://deepzz.com/post/dockerfile-reference.html#toc_6" target="_blank" rel="noopener">.dockerignore</a> 文件来排除文件和目录。</p>
</li>
<li><p>避免安装不必要的包，构建镜像应该尽可能减少复杂性、依赖关系、构建时间及镜像大小。</p>
</li>
<li><p>最小化层数。 Dockerfile的一行(除MAINTAINER外)对应镜像的一层，为使层数足够小，故可以将类似的命令串起来，比如RUN 指令，可以使用&amp;&amp;连接多个指令，如此也只有一层。</p>
</li>
<li><p>排序多行参数，通过字母将参数排序来缓解以后的变化，这将帮你避免重复的包、使列表更容易更新，如：</p>
</li>
</ul>
<figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">RUN</span><span class="bash"> apt-get update &amp;&amp; apt-get install -y \</span></span><br><span class="line"><span class="bash">  bzr \</span></span><br><span class="line"><span class="bash">  cvs \</span></span><br><span class="line"><span class="bash">  git \</span></span><br><span class="line"><span class="bash">  mercurial \</span></span><br><span class="line"><span class="bash">  subversion</span></span><br></pre></td></tr></table></figure>

<a id="more"></a>



<h3 id="0-前言"><a href="#0-前言" class="headerlink" title="0. 前言"></a>0. 前言</h3><p>容器内没有后台服务的概念。容器就是为了主进程而存在的，主进程退出，容器就失去了存在的意义，从而退出，其它辅助进程不是它需要关心的东西。所以CMD 运行可执行程序, 阻塞才可以, 要不然会退出。</p>
<h3 id="1-FROM-仓库"><a href="#1-FROM-仓库" class="headerlink" title="1. FROM 仓库"></a>1. FROM 仓库</h3><p>尽可能的使用官方仓库存储的镜像作为基础镜像。官方建议使用 <a href="https://hub.docker.com/_/debian/" target="_blank" rel="noopener">Debian</a>，大小在 150mb 左右。不过在实际开发中，应该用到 <a href="https://hub.docker.com/_/alpine/" target="_blank" rel="noopener">alpine</a> 的次数比较多，因为它仅 5mb 左右。 busybox更只有1M多。</p>
<p>参考: <a href="https://blog.csdn.net/bbwangj/article/details/81088231" target="_blank" rel="noopener">https://blog.csdn.net/bbwangj/article/details/81088231</a></p>
<h3 id="2-指令"><a href="#2-指令" class="headerlink" title="2. 指令"></a>2. 指令</h3><h5 id="2-1-COPY-和-ADD-的区别"><a href="#2-1-COPY-和-ADD-的区别" class="headerlink" title="2.1 COPY 和 ADD 的区别"></a>2.1 COPY 和 ADD 的区别</h5><p>在大多数情况下使用COPY, 使用ADD的唯一原因就是你有一个压缩文件，你想自动解压到镜像中。</p>
<h5 id="2-2-RUN-和-CMD-的区别"><a href="#2-2-RUN-和-CMD-的区别" class="headerlink" title="2.2 RUN 和 CMD 的区别"></a>2.2 RUN 和 CMD 的区别</h5><ul>
<li>RUN命令是创建Docker镜像的步骤，一个Dockerfile中可以有许多个RUN命令。</li>
<li>CMD命令是当Docker镜像被启动后Docker容器将会默认执行的命令。一个Dockerfile中只能有一个CMD命令。通过执行docker run $image other_command启动镜像可以重载CMD命令。</li>
</ul>
<h5 id="2-3-ENTRYPOINT-和-CMD的区别"><a href="#2-3-ENTRYPOINT-和-CMD的区别" class="headerlink" title="2.3 ENTRYPOINT 和 CMD的区别"></a>2.3 ENTRYPOINT 和 CMD的区别</h5><p>The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.</p>
<p>如果docker run没有指定任何的执行命令或者dockerfile里面也没有entrypoint，那么，就会使用cmd指定的默认的执行命令执行。同时也从侧面说明了entrypoint的含义，它才是真正的容器启动以后要执行命令。</p>
<ul>
<li><p>CMD的用法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">The CMD instruction has three forms:</span><br><span class="line"> </span><br><span class="line">CMD [&quot;executable&quot;,&quot;param1&quot;,&quot;param2&quot;] (exec form, this is the preferred form) //推荐</span><br><span class="line">CMD [&quot;param1&quot;,&quot;param2&quot;] (as default parameters to ENTRYPOINT)</span><br><span class="line">CMD command param1 param2 (shell form)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">eg1: CMD [&quot;/bin/bash&quot;, &quot;-c&quot;, &quot;echo &apos;hello cmd!&apos;&quot;]</span><br><span class="line">eg2: CMD [&quot;hello cmd!&quot;]</span><br><span class="line"> ENTRYPOINT [&quot;echo&quot;]</span><br><span class="line">eg3: CMD echo &quot;hello cmd!&quot;</span><br></pre></td></tr></table></figure>




</li>
</ul>
<ul>
<li><p>entrypoint的用法</p>
<p>An ENTRYPOINT allows you to configure a container that will run as an executable.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">ENTRYPOINT has two forms:</span><br><span class="line"></span><br><span class="line">ENTRYPOINT [&quot;executable&quot;, &quot;param1&quot;, &quot;param2&quot;] (exec form, preferred) //推荐</span><br><span class="line">ENTRYPOINT command param1 param2 (shell form)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">eg1: 如果命令后面有东西，那么后面的全部都会作为entrypoint的参数。如果没有，但是cmd有，那么cmd的全部内容会作为entrypoint的参数, 会输出 hello cmd 的</span><br><span class="line"></span><br><span class="line">CMD [&quot;hello cmd!&quot;]</span><br><span class="line">ENTRYPOINT [&quot;echo&quot;]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">eg2: 这个时候是不会输出 hello cmd 的</span><br><span class="line"></span><br><span class="line">CMD [&quot;hello cmd!&quot;]</span><br><span class="line">ENTRYPOINT echo</span><br></pre></td></tr></table></figure>
</li>
<li><p>覆盖问题</p>
<ul>
<li><p>cmd 除非默认, 否则轻易被覆盖</p>
</li>
<li><p>entrypoint 可以用 –entrypoint 覆盖</p>
</li>
<li><p>所以建议entrypoint固定, cmd 被覆盖, 结合使用, 并且永远使用Exec表示法</p>
</li>
</ul>
</li>
</ul>
<h5 id="2-9-环境变量写法"><a href="#2-9-环境变量写法" class="headerlink" title="2.9 环境变量写法"></a>2.9 环境变量写法</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ENV KS_HAVEN_ADDR=&apos;:16097&apos; \</span><br><span class="line">KS_HAVEN_QUIC_ADDR=&apos;:16097&apos;</span><br></pre></td></tr></table></figure>

<h3 id="3-构建镜像"><a href="#3-构建镜像" class="headerlink" title="3. 构建镜像"></a>3. 构建镜像</h3><p>在 Dockerfile 文件所在目录执行：    <code>docker build -t image_name .</code></p>
<h5 id="3-1-构建的上下文"><a href="#3-1-构建的上下文" class="headerlink" title="3.1 构建的上下文"></a>3.1 构建的上下文</h5><ol>
<li>c/s架构, 在服务端构建</li>
<li>服务端要获取文件, 需要把上下文目录打包发过去 (COPY ../package.json /app 或者 COPY /opt/xxxx /app 无法成功, 超出了上下文)</li>
<li>最好将dockerfile放在空目录下或项目根目录下, 如果没有所需文件,拷贝过来, 避免发送太多文件给引擎</li>
</ol>
<h5 id="3-2-镜像-save-load"><a href="#3-2-镜像-save-load" class="headerlink" title="3.2 镜像 save load"></a>3.2 镜像 save load</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">docker images <span class="comment">#查看构建的image</span></span><br><span class="line"></span><br><span class="line">docker save image/<span class="built_in">test</span> &gt; image_test.tar.gz <span class="comment"># save image</span></span><br><span class="line"></span><br><span class="line">docker load -i image_test.tar.gz <span class="comment"># load image</span></span><br></pre></td></tr></table></figure>

<h3 id="4-容器操作"><a href="#4-容器操作" class="headerlink" title="4. 容器操作"></a>4. 容器操作</h3><h5 id="4-1-运行和进入容器"><a href="#4-1-运行和进入容器" class="headerlink" title="4.1 运行和进入容器"></a>4.1 运行和进入容器</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">docker run -d -p 16097:16097 -p 15098:15098  image_name <span class="comment"># 启动容器</span></span><br><span class="line"></span><br><span class="line">docker extc -it 容器名字 bash <span class="comment"># 进入容器内部</span></span><br><span class="line">docker <span class="built_in">exec</span> -it 容器ID  sh   <span class="comment"># 进入容器内部</span></span><br></pre></td></tr></table></figure>

<h5 id="4-2-看容器log"><a href="#4-2-看容器log" class="headerlink" title="4.2 看容器log"></a>4.2 看容器log</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker logs -f CONTAINER_ID</span><br><span class="line"></span><br><span class="line">docker logs -f --tail=100 CONTAINER_ID</span><br></pre></td></tr></table></figure>

<h5 id="4-3-容器和宿主拷贝内容"><a href="#4-3-容器和宿主拷贝内容" class="headerlink" title="4.3 容器和宿主拷贝内容"></a>4.3 容器和宿主拷贝内容</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker cp foo.txt mycontainer:/foo.txt </span><br><span class="line">docker cp mycontainer:/foo.txt foo.txt</span><br></pre></td></tr></table></figure>

<h5 id="4-4-容器访问宿主主机端口"><a href="#4-4-容器访问宿主主机端口" class="headerlink" title="4.4 容器访问宿主主机端口"></a>4.4 容器访问宿主主机端口</h5><ul>
<li><a href="https://jingsam.github.io/2018/10/16/host-in-docker.html" target="_blank" rel="noopener">https://jingsam.github.io/2018/10/16/host-in-docker.html</a></li>
</ul>
<h3 id="5-参考资料"><a href="#5-参考资料" class="headerlink" title="5. 参考资料"></a>5. 参考资料</h3><ul>
<li><a href="https://blog.csdn.net/wdq347/article/details/78753322" target="_blank" rel="noopener">https://blog.csdn.net/wdq347/article/details/78753322</a> docker之镜像制作</li>
<li><a href="https://deepzz.com/post/dockerfile-best-practices.html" target="_blank" rel="noopener">https://deepzz.com/post/dockerfile-best-practices.html</a>  如何写好Dockerfile，Dockerfile最佳实践</li>
</ul>

    </div>

    
    
    
        
      
        <div id="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作, 一个耿直的笑脸~</div>
  <button id="reward-button" disable="enable" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
        
      
      <div style="display: inline-block">
        <img src="/images/wechatpay.jpg" alt="Levon 微信支付">
        <p>微信支付</p>
      </div>
        
      
      <div style="display: inline-block">
        <img src="/images/alipay.jpg" alt="Levon 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/linux/" rel="tag"># linux</a>
            
              <a href="/tags/docker/" rel="tag"># docker</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/p/5d303099.html" rel="next" title="golang的log库zap的使用">
                  <i class="fa fa-chevron-left"></i> golang的log库zap的使用
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/p/d319c20d.html" rel="prev" title="python基础实践">
                  python基础实践 <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          
    
    
  <div class="comments" id="comments">
    <div id="disqus_thread">
      <noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>
    </div>
  </div>
  
  

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
        
        
        
        
      

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#0-前言"><span class="nav-number">1.</span> <span class="nav-text">0. 前言</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-FROM-仓库"><span class="nav-number">2.</span> <span class="nav-text">1. FROM 仓库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-指令"><span class="nav-number">3.</span> <span class="nav-text">2. 指令</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#2-1-COPY-和-ADD-的区别"><span class="nav-number">3.0.1.</span> <span class="nav-text">2.1 COPY 和 ADD 的区别</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-2-RUN-和-CMD-的区别"><span class="nav-number">3.0.2.</span> <span class="nav-text">2.2 RUN 和 CMD 的区别</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-3-ENTRYPOINT-和-CMD的区别"><span class="nav-number">3.0.3.</span> <span class="nav-text">2.3 ENTRYPOINT 和 CMD的区别</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-9-环境变量写法"><span class="nav-number">3.0.4.</span> <span class="nav-text">2.9 环境变量写法</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-构建镜像"><span class="nav-number">4.</span> <span class="nav-text">3. 构建镜像</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#3-1-构建的上下文"><span class="nav-number">4.0.1.</span> <span class="nav-text">3.1 构建的上下文</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-2-镜像-save-load"><span class="nav-number">4.0.2.</span> <span class="nav-text">3.2 镜像 save load</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-容器操作"><span class="nav-number">5.</span> <span class="nav-text">4. 容器操作</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#4-1-运行和进入容器"><span class="nav-number">5.0.1.</span> <span class="nav-text">4.1 运行和进入容器</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#4-2-看容器log"><span class="nav-number">5.0.2.</span> <span class="nav-text">4.2 看容器log</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#4-3-容器和宿主拷贝内容"><span class="nav-number">5.0.3.</span> <span class="nav-text">4.3 容器和宿主拷贝内容</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#4-4-容器访问宿主主机端口"><span class="nav-number">5.0.4.</span> <span class="nav-text">4.4 容器访问宿主主机端口</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-参考资料"><span class="nav-number">6.</span> <span class="nav-text">5. 参考资料</span></a></li></ol></div>
        
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
      src="/images/avatar.jpg"
      alt="Levon">
  <p class="site-author-name" itemprop="name">Levon</p>
  <div class="site-description" itemprop="description">Never Give Up</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-categories">
        
          
            <a href="/categories/">
          
        
        <span class="site-state-item-count">34</span>
        <span class="site-state-item-name">分类</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
          
            <a href="/tags/">
          
        
        <span class="site-state-item-count">76</span>
        <span class="site-state-item-name">标签</span>
        </a>
      </div>
    
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://github.com/unix2dos" title="GitHub &rarr; https://github.com/unix2dos" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="mailto:levonfly@gmail.com" title="E-Mail &rarr; mailto:levonfly@gmail.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://twitter.com/levonfly" title="Twitter &rarr; https://twitter.com/levonfly" rel="noopener" target="_blank"><i class="fa fa-fw fa-twitter"></i>Twitter</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://weibo.com/l6241425" title="Weibo &rarr; https://weibo.com/l6241425" rel="noopener" target="_blank"><i class="fa fa-fw fa-weibo"></i>Weibo</a>
      </span>
    
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Levon</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">507k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">7:41</span>
</div>

        












        
      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js?v=3.1.0"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="/js/utils.js?v=7.4.0"></script><script src="/js/motion.js?v=7.4.0"></script>
<script src="/js/schemes/pisces.js?v=7.4.0"></script>

<script src="/js/next-boot.js?v=7.4.0"></script>



  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>








  <script src="/js/local-search.js?v=7.4.0"></script>














  

  

  

<script>
  function loadCount() {
    var d = document, s = d.createElement('script');
    s.src = 'https://levonfly.disqus.com/count.js';
    s.id = 'dsq-count-scr';
    (d.head || d.body).appendChild(s);
  }
  // defer loading until the whole page loading is completed
  window.addEventListener('load', loadCount, false);
</script>
<script>
  function disqus_config() {
    this.page.url = "https://unix2dos.github.io/p/2450240c.html";
    this.page.identifier = "p/2450240c.html";
    this.page.title = '记录一次docker镜像的构建过程';};
  function loadComments() {
    if (window.DISQUS) {
      DISQUS.reset({
        reload: true,
        config: disqus_config
      });
    } else {
      var d = document, s = d.createElement('script');
      s.src = 'https://levonfly.disqus.com/embed.js';
      s.setAttribute('data-timestamp', '' + +new Date());
      (d.head || d.body).appendChild(s);
    }
  }
    window.addEventListener('load', loadComments, false);
  
</script>

</body>
</html>
